home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / pas_0493.zip / ISQRT.PAS < prev    next >
Pascal/Delphi Source File  |  1993-04-15  |  2KB  |  42 lines

  1. {─ Fido Pascal Conference ────────────────────────────────────────────── PASCAL ─
  2. Msg  : 342 of 352
  3. From : Sean Palmer                         1:104/123.0          13 Apr 93  13:52
  4. To   : David Jirku
  5. Subj : Fast math
  6. ────────────────────────────────────────────────────────────────────────────────
  7.  DJ> I was just wondering how to speed up some math-intensive
  8.  DJ> routines I've got here. For example, I've got a function
  9.  DJ> that returns the distance between two objects:
  10.  
  11.  DJ> This is way to slow. I know assembly can speed it up, but
  12.  DJ> I know nothing about asm. so theres the problem. Please
  13.  DJ> help me out, any and all source/suggestions welcome!
  14.  
  15. I can whip up this integer square-root algorithm in assembly to use
  16. 386 registers... will be REAL fast! Else I could post the 16-bit version
  17. I guess...
  18.  
  19. This is tested code. Works on my 386sx fine... Auto-rounds the result. }
  20.  
  21. function iSqrt(x:longint):word;assembler;asm  {requires a 386, and TP 6.0}
  22.  db $66; xor ax,ax;            {xor eax,eax}
  23.  db $66; xor dx,dx;            {xor edx,edx}
  24.  db $66; mov di,word ptr x;    {mov edi,x}
  25.  mov cx,32;
  26. @L:
  27.  db $66; shl di,1;             {shl edi,1}
  28.  db $66; rcl dx,1;             {rcl edx,1}
  29.  db $66; shl di,1;             {shl edi,1}
  30.  db $66; rcl dx,1;             {rcl edx,1}
  31.  db $66; shl ax,1;             {shl eax,1}
  32.  db $66; mov bx,ax;            {mov ebx,eax}
  33.  db $66; shl bx,1;             {shl ebx,1}
  34.  db $66; inc bx;               {inc ebx}
  35.  db $66; cmp dx,bx;            {cmp edx,ebx}
  36.  jl @S;
  37.  db $66; sub dx,bx;            {sub edx,ebx}
  38.  db $66; inc ax;               {inc eax}
  39. @S: loop @L;
  40.  db $66; add ax,$8000; dw 0;   {add eax,$00008000}  {round result in hi word}
  41.  db $66; shr ax,16;            {shr eax,16}  {to ax}
  42.  end;